package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/repository"
	"mall-go/response"
	"strconv"
)

//分类等级
const (
	LevelOne   = 1
	LevelTwo   = 2
	LevelThree = 3
)

type CategoryController struct {
	DB *gorm.DB
}

// List 分页数据查询 根据级别和上级分类id查询
func (receiver CategoryController) List(ctx *gin.Context) {
	pageNumber, _ := strconv.Atoi(ctx.Query("pageNumber"))
	pageSize, _ := strconv.Atoi(ctx.Query("pageSize"))
	categoryLevel, _ := strconv.Atoi(ctx.Query("categoryLevel"))
	parentId, _ := strconv.Atoi(ctx.Query("parentId"))
	//参数校验
	if pageNumber < 1 || pageSize < 10 || categoryLevel < 0 || categoryLevel > 3 || parentId < 0 {
		panic("参数异常")
	}
	//封装dto
	pageDTO := dto.CategoryPageDTO{
		PageDTO: dto.PageDTO{
			Page:  pageNumber,
			Limit: pageSize,
			Start: (pageNumber - 1) * pageSize,
		},
		CategoryLevel: categoryLevel,
		ParentId:      parentId,
	}
	goodsRepo := repository.NewMallGoodsRepo()
	page := goodsRepo.GetCategoriesPage(pageDTO)
	response.Success(ctx, gin.H{"CategoryVO": page}, "Success")
}

// ListForSelect 用于三级分类联动效果制作(这个貌似没有用上)
func (receiver CategoryController) ListForSelect(ctx *gin.Context) {
	result := make(map[string][]model.GoodsCategory)
	categoryId, _ := strconv.Atoi(ctx.Query("categoryId"))
	if categoryId < 1 {
		panic("参数错误")
	}
	goodsRepo := repository.NewMallGoodsRepo()
	var category model.GoodsCategory
	//这里根据主键查询比较简单，就直接写上去了
	receiver.DB.First(&category, categoryId)
	//既不是一级分类也不是二级分类则为不返回数据
	if category.CategoryId == 0 || category.CategoryLevel == LevelThree {
		panic("参数异常")
	}
	if category.CategoryLevel == LevelOne {
		//如果是一级分类则返回当前一级分类下的所有二级分类，以及二级分类列表中第一条数据下的所有三级分类列表
		//查询一级分类列表中第一个实体的所有二级分类
		//number = 0代表所有的
		secondLevelCategories := goodsRepo.SelectByLevelAndParentIdsAndNumber([]int{categoryId}, LevelTwo, 0)
		if len(secondLevelCategories) != 0 {
			//查询二级分类列表中第一个实体的所有三级分类
			thirdLevelCategories := goodsRepo.SelectByLevelAndParentIdsAndNumber([]int{secondLevelCategories[0].CategoryId}, LevelThree, 0)
			result["secondLevelCategories"] = secondLevelCategories
			result["thirdLevelCategories"] = thirdLevelCategories
		}
	}
	if category.CategoryLevel == LevelTwo {
		//如果是二级分类则返回当前分类下的所有三级分类列表
		thirdLevelCategories := goodsRepo.SelectByLevelAndParentIdsAndNumber([]int{categoryId}, LevelThree, 0)
		result["thirdLevelCategories"] = thirdLevelCategories
	}
	response.Success(ctx, gin.H{"result": result}, "Success")
}

// Save 增加
func (receiver CategoryController) Save(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.GoodsCategoryAddParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	//校验
	if *param.ParentId < 0 {
		panic("参数异常")
	}
	//增加操作
	goodsRepo := repository.NewMallGoodsRepo()
	flag := goodsRepo.SaveCategory(param, adminUserId)
	if flag {
		response.Success(ctx, nil, "添加成功")
		return
	}
	response.Failure(ctx, nil, "添加失败")
}

// Update 修改
func (receiver CategoryController) Update(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.GoodsCategoryEditParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	//校验
	if *param.ParentId < 0 {
		panic("参数异常")
	}
	//执行更新操作
	goodsRepo := repository.NewMallGoodsRepo()
	flag := goodsRepo.UpdateGoodsCategory(param, adminUserId)
	if flag {
		response.Success(ctx, nil, "更新成功")
	} else {
		response.Failure(ctx, nil, "更新失败")
	}
}

// Info 获取信息
func (receiver CategoryController) Info(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Params.ByName("id"))
	var category model.GoodsCategory
	//这里直接查主键就不做封装了
	receiver.DB.First(&category, id)
	if category.CategoryId == 0 {
		response.Failure(ctx, nil, "数据不存在")
	} else {
		response.Success(ctx, gin.H{"category": category}, "查询成功")
	}
}

// BatchDelete 批量删除
func (receiver CategoryController) BatchDelete(ctx *gin.Context) {
	var param dto.BatchIdParam
	ctx.Bind(&param)
	if len(param.UserIds) < 1 {
		panic("参数错误")
	}
	//执行批量删除
	goodsRepo := repository.NewMallGoodsRepo()
	goodsRepo.BatchDelete(param)
	response.Success(ctx, nil, "删除成功")
}

func NewCategoryController() CategoryController {
	db := common.GetDB()
	return CategoryController{DB: db}
}
